using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Data.Security;
using BLL = Auto.Data.Systems.BLL;

namespace BaseSystem._System.UserControl
{
    public partial class ucRoles : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public string SystemID
        {
            get { return (ViewState["SystemID"] == null) ? "" : ViewState["SystemID"].ToString(); }
            set
            {
                ViewState["SystemID"] = value;
                GridView1.DataSource =new SysRolesManager().GetSysRoles(SystemID); //SystemRoles.Instance.GetList("SysID='"+SystemID+"'");
                GridView1.DataBind();
            }
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "EditRole":
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    System.Drawing.Color selectedbackcolor = System.Drawing.ColorTranslator.FromHtml("#d7e3f6");

                    for (int i = 0; i < GridView1.Rows.Count; i++)
                    {
                        if (i == rowIndex)
                        {
                            GridView1.Rows[i].BackColor = selectedbackcolor;
                        }
                        else
                        {
                            GridView1.Rows[i].BackColor = System.Drawing.Color.Empty;
                        }
                    }
                    string sysID = (GridView1.Rows[rowIndex].FindControl("lblSysID") as Label).Text;
                    string roleID = (GridView1.Rows[rowIndex].FindControl("lblRoleID") as Label).Text;
                    try
                    {
                        SysRoles modelSysRole = new SysRoles(sysID, roleID);
                        RoleMenuMethod(modelSysRole, new BLL.Systems().GetModel(sysID).SysName);

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    break;
                
                default: break;
            }
        }
        private bool RoleMenuVisible
        {
            set
            {

                Control ucRolesMenu = GetControlByID("UcRoleMenu1");
                if (ucRolesMenu != null)
                {
                    ucRolesMenu.Visible = value;
                }
            }
        }
        private void RoleMenuMethod(SysRoles modelSysRole,string sysName)
        {
            RoleMenuVisible = true;
            Type t = GetControlByID("UcRoleMenu1").GetType();
            Control ucRolesMenu = GetControlByID("UcRoleMenu1");
            System.Reflection.MethodInfo methodInfo = t.GetMethod("Display");

            object[] obj = new object[2];
            obj[0] = modelSysRole;
            obj[1] = sysName;
            methodInfo.Invoke(ucRolesMenu, obj);
        }
        private Control GetControlByID(string controlID)
        {
            return Page.Form.FindControl("ContentMain").FindControl(controlID);
        }

        protected void GridView1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridView1.Rows[i].BackColor = System.Drawing.Color.Empty;
            }
        }

    }
}